﻿using HCMS.CommonDtos;
using HCMS.Const;
using HCMS.Entities.Manage;
using Microsoft.Extensions.Caching.Distributed;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Services;

namespace HCMS.DomainServiceManage.Cache.Manage
{
    /// <summary>
    /// 角色 菜单 缓存 域服务
    /// </summary>
    public class RoleMenuCacheManager : DomainService
    {
        private readonly IDistributedCache cache;

        public RoleMenuCacheManager(IDistributedCache cache)
        {
            this.cache = cache;
        }

        /// <summary>
        /// 设置 菜单列表 信息
        /// </summary>
        /// <param name="roleId">角色 Id</param>
        /// <param name="menu">菜单列表</param>
        /// <returns></returns>
        public async Task<bool> SetMenuList(int roleId, List<Menu> menu)
        {
            if (menu == null || !menu.Any()) { return false; }

            string serializeValule = JsonConvert.SerializeObject(menu);
            byte[] storeValue = Encoding.UTF8.GetBytes(serializeValule);
            DistributedCacheEntryOptions options = new DistributedCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromHours(6));
            await cache.SetAsync(string.Format(Manage_RedisKey.RoleMenuList, roleId), storeValue, options);

            return true;
        }

        /// <summary>
        /// 获取 菜单列表 信息
        /// </summary>
        /// <param name="roleId">角色 Id</param>
        /// <returns></returns>
        public async Task<List<Menu>> GetMenuList(int roleId)
        {
            string cacheValue = (await cache.GetStringAsync(string.Format(Manage_RedisKey.RoleMenuList, roleId))) ?? "";
            if (string.IsNullOrWhiteSpace(cacheValue))
            {
                return new List<Menu>();
            }

            var result = JsonConvert.DeserializeObject<List<Menu>>(cacheValue);
            return result ?? new List<Menu>();
        }

        /// <summary>
        /// 设置 管理系统 菜单
        /// </summary>
        /// <param name="roleId">角色 Id</param>
        /// <param name="menu">菜单信息</param>
        /// <returns></returns>
        public async Task<bool> SetManageMenu(int roleId, List<ManageMenuTreeDto> menu)
        {
            string serializeValule = JsonConvert.SerializeObject(menu);
            byte[] storeValue = Encoding.UTF8.GetBytes(serializeValule);
            DistributedCacheEntryOptions options = new DistributedCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromHours(6));
            await cache.SetAsync(string.Format(Manage_RedisKey.RoleMenuTree, roleId), storeValue, options);

            return true;
        }

        /// <summary>
        /// 获取 管理系统 菜单
        /// </summary>
        /// <param name="roleId">角色 Id</param>
        /// <returns></returns>
        public async Task<List<ManageMenuTreeDto>> GetManageMenu(int roleId)
        {
            string cacheValue = (await cache.GetStringAsync(string.Format(Manage_RedisKey.RoleMenuTree, roleId))) ?? "";
            if (string.IsNullOrWhiteSpace(cacheValue))
            {
                return new List<ManageMenuTreeDto>();
            }

            var result = JsonConvert.DeserializeObject<List<ManageMenuTreeDto>>(cacheValue);
            return result ?? new List<ManageMenuTreeDto>();
        }
    }
}
